package com.ray.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.ReportBean;
import com.ray.exception.ServiceException;
import com.ray.pojo.MyUser;
import com.ray.service.ApprovalService;
import com.ray.service.CompanyMaterialService;
import com.ray.service.DisplayViewService;
import com.ray.service.EquipmentService;
import com.ray.service.OrderService;
import com.ray.service.OtherWarehouseService;
import com.ray.service.WorkshopService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

/**
 * 显示屏
 * @author nate
 *
 */
@Controller
@RequestMapping("/displayView")
public class DisplayViewController {
	
	@Autowired
	DisplayViewService displayViewService;
	@Autowired
	WorkshopService workshopService;
	@Autowired
	EquipmentService equipmentService;
	
	
    /**
     * 设置显示屏登录令牌
     * @param request
     * @return
     * @throws ServiceException
     */
	@RequestMapping("/setDisplayViewToken")
	@ResponseBody
	public Map setDisplayViewToken(HttpServletRequest request) throws ServiceException{
		MyUser myUser=RequestUtil.getLoginUser(request);
		String token=displayViewService.setDisplayViewToken(myUser);
		return ResponseUtil.ResponseResult(token);
	}
	
	
	/**
	 * 获取当月日产能
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/queryCapacity")
	@ResponseBody
	public Map queryCapacity(HttpServletRequest request,String token) throws ParseException, IOException {
//		Assert.notNull(null,"看板升级中，暂停使用。。");
		MyUser myUser= displayViewService.checkLogin(request,token);
		Map map=RequestUtil.getRequestAllParam(request);
		map=this.setParam(map,myUser);
		return ResponseUtil.ResponseResult(workshopService.queryCapacityFromRedis(map));
	}
	
	
	/**
	 * 获取车间工序日产能
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/getMachieProcessCapacity")
	@ResponseBody
	public Map getMachieProcessCapacity(HttpServletRequest request,String token) throws ParseException, IOException {
//		Assert.notNull(null,"看板升级中，暂停使用。。");
		MyUser myUser= displayViewService.checkLogin(request,token);
		Map map=RequestUtil.getRequestAllParam(request);
//		map=this.setParam(map,myUser);
		return ResponseUtil.ResponseResult(equipmentService.getMachieProcessCapacity(map));
	}
	
	
	/**
	 * 获取设备工序产能
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/queryMacineCapacity")
	@ResponseBody
	public Map queryMacineCapacity(HttpServletRequest request,String token) throws ParseException, IOException {
//		Assert.notNull(null,"看板升级中，暂停使用。。");
		MyUser myUser= displayViewService.checkLogin(request,token);
		Map map=RequestUtil.getRequestAllParam(request);
//		map=this.setParam(map,myUser);
		return ResponseUtil.ResponseResult(workshopService.queryMacineCapacityRedis(map));
	}
	
	
	/**
	 * 获取周转量，尾数，拖期异常
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/getTrunoverDutyMantissa")
	@ResponseBody
	public Map getTrunoverDutyMantissaFromRedis(HttpServletRequest request,String token) throws ParseException, IOException {
		MyUser myUser= displayViewService.checkLogin(request,token);
		Map map=RequestUtil.getRequestAllParam(request);
		map=this.setParam(map,myUser);
		return ResponseUtil.ResponseResult(displayViewService.getTrunoverDutyMantissaFromRedis(map,myUser));
//		return ResponseUtil.ResponseResult("");
	}
	
	
	/**
	 * 采购拖期异常
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/getPurchaseOrderDutyData")
	@ResponseBody
	public Map getPurchaseOrderDutyData(HttpServletRequest request,String token) throws ParseException, IOException {
		MyUser myUser= displayViewService.checkLogin(request,token);
		Map map=RequestUtil.getRequestAllParam(request);
		map=this.setParam(map,myUser);
		return ResponseUtil.ResponseResult(displayViewService.getPurchaseOrder(map));
	}
	
	
	/**
	 * 获取仓库库存超出最大范围
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/warehouseBeyondMax")
	@ResponseBody
	public Map warehouseBeyondMax(HttpServletRequest request) throws ParseException, IOException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.warehouseBeyondMax(map));
	}
	
	/**
	 * 获取仓库库存超出最大范围
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/warehouseBeyondMin")
	@ResponseBody
	public Map warehouseBeyondMin(HttpServletRequest request) throws ParseException, IOException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.warehouseBeyondMin(map));
	}
	
	
	/**
	 * 获取呆滞物料
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/getStillnessMaterial")
	@ResponseBody
	public Map getStillnessMaterial(HttpServletRequest request) throws ParseException, IOException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.getStillnessMaterial(map));
	}
	
	
	/**
	 * 获取订单设备生产进度看板
	 * @param request
	 * workShop 车间id
	 * equipmentNo 设备编号
	 * orderId 订单编号
	 * userId 操作人
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/getEquipmentChangeOrderRecord")
	@ResponseBody
	public Map getEquipmentChangeOrderRecord(HttpServletRequest request) throws ParseException, IOException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.getEquipmentChangeOrderRecord(map));
	}
	
	
	/**
	 * 获取呆滞物料
	 * @param request
	 * @return
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws ServiceException
	 */
	@RequestMapping("/getReceivablesDelayData")
	@ResponseBody
	public Map getReceivablesDelayData(HttpServletRequest request) throws ParseException, IOException {
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.getReceivablesDelayData(map));
	}
	
	
	/**
	 * 退出登录
	 * @throws ParseException 
	 * @throws ServiceException
	 */
	@RequestMapping("/loginOut")
	public String loginOut(HttpServletRequest request,String token) throws ParseException {
		request.getSession().setAttribute("userLogin", null);
		return "redirect:login.html";
	}
	
	/**
	 * 退出登录
	 * @throws ParseException 
	 * @throws ServiceException
	 */
	@RequestMapping("/getDate")
	@ResponseBody
	public Map getDate(HttpServletRequest request) throws ParseException {
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return ResponseUtil.ResponseResult(simpleDateFormat.format(new Date()));
	}
	
	
	/**
	 * 退出登录
	 * @throws ParseException 
	 * @throws ServiceException
	 */
	@RequestMapping("/getQCDisplay")
	@ResponseBody
	public Map getQCDisplay(HttpServletRequest request) throws ParseException {
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.getQCDisplay(map));
	}
	
	
	/**
	 * 获取人员工序看板
	 * month
	 * @throws ParseException 
	 * @throws ServiceException
	 */
	@RequestMapping("/getUserProcessDisplayViewData")
	@ResponseBody
	public Map getUserProcessDisplayViewData(HttpServletRequest request) throws ParseException {
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.getUserProcessDisplayViewData(map));
	}
	
	/**
	 * 设置物料是否显示
	 * @return
	 */
	@RequestMapping("/setMaterialIsShow")
	@ResponseBody
	public Map setMaterialIsShow(HttpServletRequest request) {
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.setMaterialIsShow(map));
	}
	
	
	/**
	 * 设置物料是否显示
	 * @return
	 */
	@RequestMapping("/getMaterialIsShow")
	@ResponseBody
	public Map getMaterialIsShow(HttpServletRequest request) {
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.getMaterialIsShow(map));
	}
	
	/**
	 * 获取配套看板数据
	 * @param request
	 * @return
	 * @throws CloneNotSupportedException 
	 * @throws ParseException 
	 */
	@RequestMapping("/getOrderMatchingDetail")
	@ResponseBody
	public Map getOrderMatchingDetail(HttpServletRequest request) throws ParseException, CloneNotSupportedException {
		Map map =RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(displayViewService.getOrderMatchingDetail(map));
	}

	
    
	
	

	/**
	 * 设置登录人的参数
	 * @param map
	 * @param myUser
	 * @return
	 */
	private Map setParam(Map map, MyUser myUser) {
		/**
		 * 如果职位是经理
		 */
		if(1==myUser.getIsManager()) {
			
		}else if("1".equals(myUser.getDuty())) {
			Assert.isTrue(null!=myUser.getDeptId() && !myUser.getDeptId().equals(""),myUser.getUserName()+"经理还没有设置部门，请至职员表设置部门");
			map.put("deptId", myUser.getDeptId());
		}else if("2".equals(myUser.getDuty())) {
			Assert.isTrue(null!=myUser.getWorkShopId() && !myUser.getWorkShopId().equals(""),myUser.getUserName()+"主管还没有设置车间，请至职员表设置车间");
			map.put("workShopId", myUser.getWorkShopId());
		}else {
			Assert.notNull(null,"该页面只有注册人、经理、主管能访问，请至职员表设置职位");
		}
		return map;
	}

	
	
	
	
	

}
